package edu.sctu.zhuyingzhi.month201506.day20150615.fileOperationV2;

import java.io.*;
import java.util.Scanner;

/**
 * Created by user13 on 2015/6/14.
 */
public class FileUitl {
    Scanner in = new Scanner(System.in);

    public FileUitl(){

    }


    /**
     * 打开文件
     * @param list
     * @throws IOException
     */
    public void openFile(String list) throws IOException {
        File file = new File(list);
        if (file.exists()){
        java.awt.Desktop.getDesktop().open(file);}
        else {
            System.out.println("无此路径 请重新选择");
        }
    }


    /**
     * 复制文件
     * @param file
     * @param targetFile
     * @throws IOException
     */
    public void copyFile(String file,String targetFile) throws IOException {
        int index = file.lastIndexOf("/");
        File file1 = new File(targetFile);
        File file3 = new File(file);
        File file2 = new File(targetFile+file.substring(index));
        if (!file1.exists()){
            file1.mkdirs();
        }
        FileInputStream in = new FileInputStream(file3);
        FileOutputStream out = new FileOutputStream(file2);
        int count;
        byte[] buffer = new byte[1024];
        while((count = in.read(buffer)) != -1){
            out.write(buffer,0,count);
        }
        out.close();
        in.close();

        if (file3.exists() && file2.exists()){
            System.out.println("文件复制成功！");
        }
        else{
            System.out.println("文件复制失败 ");
        }

    }

    /**
     * 移动文件
     * @param file
     * @param targetFile
     * @throws IOException
     */
    public void moveFile(String file,String targetFile) throws IOException {
        int index = file.lastIndexOf("/");
        File file2 = new File(file);
        File file1 = new File(targetFile);
        File file3 = new File(targetFile+file.substring(index));
        if (!file1.exists()){
            file1.mkdirs();
        }
        FileInputStream in = new FileInputStream(file2);
        FileOutputStream out = new FileOutputStream(file3);
        int count;
        byte[] buffer = new byte[1024];
        while((count = in.read(buffer)) != -1){
            out.write(buffer,0,count);
        }
        out.close();
        in.close();
        file2.delete();
        if(file3.exists() && !file2.exists()){
            System.out.println("移动文件成功");
        }
        else{
            System.out.println("失败");
        }
    }

    /**
     * 删除文件
     * @param file
     * @throws IOException
     */
    public void deleteFile(String file) throws IOException {
        File file1 = new File(file);
        file1.delete();
        if (!file1.exists()){
            System.out.println("删除文件成功|");
        }
        else{
            System.out.println("删除失败");
        }
    }

    /**
     * 查看文件夹
     * @param file
     */
    public void readFile(String file){
        File file1 = new File(file);
        if(file1.isDirectory()){
            File[] files = file1.listFiles();
            for (File file2 :files){
                System.out.println(file2.getAbsolutePath());
            }
        }

    }


//    public boolean judgeFile(File file){
//        if (!file.exists()){
//            file.mkdirs();
//        }
//        return file.exists();
//    }
//
//    public boolean creatFile(File file1) throws IOException {
//
//
//
//            file1.createNewFile();
//
//
//
//        return file1.exists();
//    }
//
//    public void writeFile(File file) throws IOException {
//
//        FileWriter out = new FileWriter(file);
//        char[] json_data = new char[50];
//        for (int i = 0; i < 50; i++) {
//            json_data[i] = 50;
//        }
//        System.out.println(json_data);
//        out.write(json_data, 0, json_data.length);
//        out.flush();
//        out.close();
//
//    }


    }
